<?php
namespace app\index\controller;

use app\admin\controller\Permissions;
use app\model\CashoutNote;
use app\model\Order;
use app\model\User;
use think\Controller;
use think\Db;
use think\Request;

class Index extends Permissions
{
    public function index()
    {
        $data = $this->request->param("time");
        $date = $data['time'] ? $data['time'] : date("Y-m-d");
        $user = new User();
        //今日注册用户
        $today_user_where['create_time'] = ['between',[strtotime($date." 00:00:00"),strtotime($date." 23:59:59")]];
        $count_data['today_user'] = $user->where($today_user_where)->count();
        //今日注册用户
        $today_active_user_where['create_time'] = ['between',[strtotime($date." 00:00:00"),strtotime($date." 23:59:59")]];
        $today_active_user_where['online_time'] =['<>',null];
        $count_data['today_active_user'] = $user->where($today_active_user_where)->count();

        //今日在线用户
        $today_online_user_where['online_time'] =['<>',null];
        $count_data['today_online_user'] = User::where($today_online_user_where)->count();

        //今日收入金额
        $today_money_where['payment_time'] = ['between',[strtotime($date." 00:00:00"),strtotime($date." 23:59:59")]];
        $count_data['today_money'] = Order::where('status', 1)->where($today_money_where)->sum('amount');
        $count_data['today_ad_money'] = Order::where('status', 1)->where('name', '代发广告')->where($today_money_where)->sum('amount');
        $count_data['today_gold_money'] = Order::where('status', 1)->where('name', '充值积分')->where($today_money_where)->sum('amount');
        $count_data['today_vip_money'] = Order::where('status', 1)->where('name', '开通会员')->where($today_money_where)->sum('amount');
        $count_data['today_partner_money'] = Order::where('status', 1)->where('name', '开通代理')->where($today_money_where)->sum('amount');
        $today_cashout_money_where ['update_time'] = ['between',[strtotime($date." 00:00:00"),strtotime($date." 23:59:59")]];
        $count_data['today_cashout_money'] = CashoutNote::where('status', 1)->where($today_cashout_money_where)->sum('amount');

        $this->assign("data",$count_data);
        return $this->fetch();
    }


    public function getUserTrendData()
    {
        if($this->request->isPost()){
            $data = $this->request->param("time");
            $date = $data['time'] ? $data['time'] : date("Y-m-d");

            $trend_data['date'] = [];
            for($i=0; $i<24; $i++){
                $trend_data['date'][] =$i;
                $i = str_pad($i,2,"0",STR_PAD_LEFT);
                $where['create_time'] = ['between',[strtotime($date." $i:00:00"),strtotime($date." 23:59:59")]];
                $trend_data['user_num'][] = User::where($where)->count();

                $where2['online_time'] = ['between',[strtotime($date." $i:00:00"),strtotime($date." 23:59:59")]];
                $trend_data['online_num'][] = User::where($where)->count();
            }

            return $this->responseAjax(200,"",0,$trend_data);
        }

    }


    public function getOrderTrendData()
    {
        if ($this->request->isPost()) {
            $date = input('get.date', date('Y-m-d'));
            $date = date('Y-m-d', strtotime($date));

            $trend_data['date'] = [];
            for ($i = 0; $i < 24; $i++) {
                $trend_data['date'][] = $i;
                $i = str_pad($i, 2, "0", STR_PAD_LEFT);
                $where['payment_time'] = ['between', [strtotime($date . " $i:00:00"), strtotime($date . " 23:59:59")]];
                $trend_data['pay_count'][] = Order::where('status', 1)->where($where)->sum('amount');
            }

            foreach ($trend_data['pay_count'] as $key => $item) {
                $trend_data['pay_count'][$key] = $item / 100;
            }

            return $this->responseAjax(200, "", 0, $trend_data);
        }
    }
}
